/*
Copyright (C) 2010 Martin Zuber, zuber@centrum.cz

This application is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.

This application is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with This application; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QResizeEvent>
#include <QLayoutItem>
#include <QAction>
#include <QMenuBar>

#include "feggclockwidget.h"
#include "dsettings.h"

/**
  Initializes the main form, connects signals.

  @param parent parent widget, should be null
  */
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    grid = new QGridLayout(this);

    m_clocks = new FEggClockWidget* [3];        

    for(int i = 0; i < 3; i++) {
       m_clocks[i] = new FEggClockWidget();
    }

    m_option = new QAction(tr("Options"), this);
    m_option->setSoftKeyRole(QAction::PositiveSoftKey);
    connect(m_option, SIGNAL(triggered()), this, SLOT(onOptionTriggered()));
    addAction(m_option);

    // workaround: the softkeys are not visible in simulator
#if defined(Q_WS_SIMULATOR)
    QMenuBar *menubar = new QMenuBar(this);
    menubar->addAction(m_option);
    this->setMenuBar(menubar);
#endif
 }

/**
  Delete all dynamic data
  */
MainWindow::~MainWindow()
{
    // delete widgets
    for(int i = 0; i < 3; i++) delete m_clocks[i];

    // delete array
    delete m_clocks;

    // delete ui
    delete ui;
}

/**
  Resize handling, there are two modes:
  <ul>
    <li>portrait mode - there are 3 widgets in one column</li>
    <li>landscape mode - 2 widgets in one row</li>
  </ul>

  When the main form is in portrait mode, the 3rd widget
  is stopped, reset and hidden.

  @param e resize event data
  */
void MainWindow::resizeEvent(QResizeEvent *e)
{
    // get the size parts
    int w = this->size().width();
    int h = this->size().height();

    // delete old grid
    delete grid;

    // create new one
    // TODO: is there way to "reconfigure" the widgets in a grid?
    grid = new QGridLayout();

    for(int i = 0; i < 2; i++) grid->removeWidget(m_clocks[i]);
    if(w > h)
    {
        // landscape mode - there is only place for 2 clock widgets
        // TODO: Verify on real device the 2x2 matrix
        grid->addWidget(m_clocks[0], 0, 0);
        grid->addWidget(m_clocks[1], 1, 0);

        // invisible item is stopped and reset
        m_clocks[2]->stopAlertAndReset();
        // ... and hidden
        m_clocks[2]->setVisible(false);
    }
    else
    {
        // portrait mode - 3 widgets in one column are enough
        grid->addWidget(m_clocks[0], 0, 0);
        grid->addWidget(m_clocks[1], 1, 0);
        grid->addWidget(m_clocks[2], 2, 0);

        // the last one should be visible
        m_clocks[2]->setVisible(true);
    }

    // place grid and layout
    ui->centralWidget->setLayout(grid);
    grid->layout();
}

void MainWindow::onOptionTriggered()
{
    qDebug() << "Option action triggered";

    DSettings dlg(this);
    dlg.exec();
}
